package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyCommentService strategyCommentService;


    /**
     * 查询所有已发布的攻略
     *
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo<Strategy> list(StrategyQueryObject qo) {
        //查询所有非禁用的攻略
        PageInfo pageInfo = strategyService.query(qo);
        return pageInfo;
    }

    @GetMapping("/{id}")
    public Strategy get(@PathVariable Long id) {
        return strategyService.get(id);
    }

    @GetMapping("/{strategyId}/catalogs")
    public List<StrategyCatalog> listStrategyCatalogsByStrategyId(@PathVariable Long strategyId) {
        return strategyCatalogService.listByStrategyId(strategyId);
    }

    /**
     * 查询指定大攻略下的所有评论信息
     *
     * @param qo
     * @return
     */
    @GetMapping("/{StrategyId}/comments")
    public PageInfo<Strategy> listCommonds(StrategyQueryObject qo) {
        PageInfo pageInfo = strategyCommentService.query(qo);
        return pageInfo;
    }

    /**
     * 保存买某个攻略的评论
     *
     * @param
     * @return
     */
    @PostMapping("/{strategyId}/comments")
    public JsonResult saveComments(StrategyComment comment, String[] tags) {
        JsonResult result = new JsonResult();
        strategyCommentService.saveOrUpdate(comment, tags);
        return result;
    }


}
